package com.rh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.rh.entity.TOrder;
import com.rh.exception.EducationException;
import com.rh.result.ResultVo;
import com.rh.service.TOrderService;
import com.rh.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单 前端控制器
 * @author rh
 */
@RestController
@RequestMapping("/edeorder/order")
@Api(tags = "订单管理")
public class TOrderController {
    @Autowired
    private TOrderService tOrderService;

    /**
     * 1.生成订单的方法
     * 根据课程id生成订单
     */
    @ApiOperation(value = "1.生成订单的方法")
    @GetMapping("createOrder/{courseId}")
    public ResultVo createOrder(@PathVariable String courseId, HttpServletRequest request){
        //获得请求头中的id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断是否登录
        if (StringUtils.isEmpty(memberId)){
            throw new EducationException(20001,"还没登陆,请先登陆!");
        }
        //创建订单,返回一个订单号
        String orderNo=tOrderService.createOrders(courseId,memberId);
        return ResultVo.ok().data("orderNo",orderNo);
    }
    /**
     * 根据订单号查询订单
     */
    @ApiOperation(value = "2.根据订单id查询订单")
    @GetMapping("getOrderInfo/{orderId}")
    public ResultVo getOrderInfo(@PathVariable String orderId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        //订单号是工具类生成出来的
        wrapper.eq("order_no",orderId);
        TOrder order = tOrderService.getOne(wrapper);
        return ResultVo.ok().data("item",order);
    }
}

